package com.lsh.oms.core.proxy.service;

import com.alibaba.fastjson.JSON;
import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.utils.HttpUtils;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * Project Name: lsh-oms
 * Created by peter on 18/2/27.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Component
public class PoOmsProxy extends BaseProxy {

    private static final String po_method_url = "/goods/supply/item2codeforfood";
    private static final String check_mothod = "/API/V1/openApi/getPoInfoByWumartCxId";
    private static Logger logger = LoggerFactory.getLogger(TmsProxy.class);
    @Value("${po.oms.host}")
    private String poHost;

    @Value("${wms.server.proxy.path}")
    private String wmsHost;

    public HttpMessage itemCode2wmCode(Map<String, Object> params) {
        String url = poHost + po_method_url;

        String message = "[poGoodsProxy][itemCode2wmCode]";
        HttpMessage httpMessage = null;

        try {

            logger.info("{}[start][Request url]:{}", message, url);
            httpMessage = HttpUtils.sendPostRequest(url, params, this.getRequestHeader());
            System.out.println(JSON.toJSONString(httpMessage));
            logger.info("{}[end][Response result]:{}", message, JSON.toJSONString(httpMessage));

        } catch (IOException e) {

            logger.error("{}[error][Error message]:{}", message, e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {

            logger.error("{}[error][Error message]:{}", message, e.getMessage());
            logger.error(e.getMessage(), e);
        }

        return httpMessage;
    }


    private Header[] getRequestHeader() {
        Header[] headers = {new BasicHeader("Accept", "application/json")};
        return headers;
    }

    private Header[] getWmsRequestHeader() {
        Header[] headers = {new BasicHeader("Content-Type", "application/json")};
        return headers;
    }


    public HttpMessage checkPoNo(String paramJson) {
        String url = wmsHost + check_mothod;

        String message = "[poWmsProxy][checkPoNo]";
        HttpMessage httpMessage = null;

        try {

            logger.info("{}[start][Request url]: {}", message, url);
            httpMessage = HttpUtils.sendPostRequest(url, paramJson, this.getWmsRequestHeader());
            logger.info("{}[end][Response result]: {}", message, JSON.toJSONString(httpMessage));

        } catch (IOException e) {

            logger.error("{}[error][Error message]:{}", message, e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {

            logger.error("{}[error][Error message]:{}", message, e.getMessage());
            logger.error(e.getMessage(), e);
        }

        return httpMessage;
    }
}
